/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import dao.exceptions.NonexistentEntityException;
import dao.exceptions.PreexistingEntityException;
import dominio.CompromissoMd;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author devair
 */
public class CompromissoJpaController {

    public CompromissoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }        
    
    private EntityManagerFactory emf = null;
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    public void create(CompromissoMd compromisso) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(compromisso);
            em.merge(compromisso); //na classe compromisso CompromissoMd Cascade.MERGE
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCompromisso(compromisso.getId()) != null) {
                throw new PreexistingEntityException("compromisso " + compromisso + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CompromissoMd compromisso) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            compromisso = em.merge(compromisso);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = compromisso.getId();
                if (findCompromisso(id) == null) {
                    throw new NonexistentEntityException("The compromisso with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CompromissoMd compromisso;
            try {
                compromisso = em.getReference(CompromissoMd.class, id);
                compromisso.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The compromisso with id " + id + " no longer exists.", enfe);
            }
            em.remove(compromisso);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CompromissoMd> findCompromissoEntities() {
        return findCompromissoEntities(true, -1, -1);
    }             
    
    public List<CompromissoMd> findCompromissoEntities(String filtro) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            return em.createNamedQuery("filtrocompromisso").setParameter("texto", "%" + filtro + "%").getResultList();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
   

    public List<CompromissoMd> findCompromissoEntities(int maxResults, int firstResult) {
        return findCompromissoEntities(false, maxResults, firstResult);
    }

    private List<CompromissoMd> findCompromissoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CompromissoMd.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CompromissoMd findCompromisso(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CompromissoMd.class, id);
        } finally {
            em.close();
        }
    }

    public int getCompromissoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CompromissoMd> rt = cq.from(CompromissoMd.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
